#include "NMPreHeader.h"
#include "NMEnemyBullet.h"
#include "NMActionMgr.h"

NMEnemyBullet::NMEnemyBullet(CCSpriteBatchNode* batch)
{
	FUNCTION_ENTER();

	power_ = 20 + rand() % 10;
	speed_ = 40;
	destroied_ = false;

	CCRect rc = CCRectMake(62,0,8,20);
	initWithBatchNode(batch, rc);

	autorelease();

	FUNCTION_LEAVE();
}

NMEnemyBullet::~NMEnemyBullet(void)
{
	FUNCTION_ENTER();

	FUNCTION_LEAVE();
}

void NMEnemyBullet::update(ccTime dt)
{
	FUNCTION_ENTER();

	CCPoint pos = getPosition();
	pos.y -= speed_ * dt;
	setPosition(pos);

	FUNCTION_LEAVE();
}

void NMEnemyBullet::destroy()
{
	FUNCTION_ENTER();

	destroied_ = true;
	setScale(0.5);
	CCAction* action = 	CCSequence::actions(
		CCAnimate::actionWithAnimation(NMActionMgr::shared()->getAniBulletTarget(bulletType_Normal), false),
		CCCallFunc::actionWithTarget(this, callfunc_selector(NMEnemyBullet::_trueDestroy)),
		NULL);

	runAction(action);

	FUNCTION_LEAVE();
}

void NMEnemyBullet::_trueDestroy()
{
	FUNCTION_ENTER();

	stopAllActions();
	if (getParent())
		removeFromParentAndCleanup(true);

	FUNCTION_LEAVE();
}
